package com.supermap.business.policy_developments;

import com.supermap.dataservice.util.HttpRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author lxs
 * @Title:农业接口封装
 * @Description:
 * @date 2018/4/17  15:28
 */
@Controller
@RequestMapping("AgricultureController")
public class AgricultureController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AgricultureController.class);

    /**
     * 政策动态
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getDynamic")
    @ResponseBody
    public String getDynamic(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://nyzc.xn121.com/zcdt/daily.htm";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     * 农气新闻
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getNews")
    @ResponseBody
    public String getNews(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://nyqx.xn121.com/nqxw/lb/daily.htm?1523931740764";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     * 农用天气预报
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getWeather")
    @ResponseBody
    public String getWeather(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://nyqx.xn121.com/nytqyb/daily.htm?1523931844457";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     *农业科技 国内最新农业技术推广
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getTechnology")
    @ResponseBody
    public String getTechnology(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://nykj.xn121.com/gnzxnyjstg/daily.htm?1523931773314";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     * 农业保险动态
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getInsurance")
    @ResponseBody
    public String getInsurance(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://www.xn121.com/nybx/nybxdt/daily.htm?1523931895811";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     * 农业保险 理赔案例
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getCase")
    @ResponseBody
    public String getCase(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("", "");
        response.setHeader("", "");
        String url = "http://www.xn121.com/nybx/lpal/daily.htm?1523931931876";
        String result = HttpRequest.httpURLConectionGET(url);
        return result;
    }

    /**
     * 爬取农业政策
     * @return
     */
    @RequestMapping("/ReptilianDynamic")
    @ResponseBody
    public String ReptilianDynamic(){
        // 定义即将访问的链接
        String url = "http://nyzc.xn121.com/zcdt/04/1907205.shtml";
        String result = HttpRequest.httpURLConectionGET(url);
      // 解析一个HTML文档
        Document doc = Jsoup.parse(result);
        Elements rows = doc.select("div[class=area]").get(0).select("div[class=col650 col650Bg]").get(0).select("div[class=col650Box]").get(0).select("div[class=baike_7 zuy_1]");
       return rows.html();
    }

    /**
     * 格点数据
     * @param request
     * @return
     */
    @RequestMapping("/getPES")
    @ResponseBody
    public HashMap<String,String> getPES(HttpServletRequest request){
        HashMap hashMap = new HashMap();
        String lng=request.getParameter("lng");
        String lat=request.getParameter("lat");
        String [] types={"WS","WD","tmp","rain","rrh"};
        String url=null;
        for (int i = 0; i <types.length ; i++) {
             url = "http://open.xn121.com:8090/MyInterfaceNew/PES?appid=7670a3&key=fd6bb00ee7f53c799a23eef30d851b85&lng=";
             url +=lng+"&lat="+lat+"&type="+types[i];
            String result = HttpRequest.httpURLConectionGET(url);
            hashMap.put(types[i],result);
        }
        return hashMap;
    }
}
